
package com.studiofortress.sf.util.structure;

import com.studiofortress.sf.graphics.GLTexture;
import com.studiofortress.sf.graphics.GraphicsGL;

/**
 * A version of the ButtonActor that uses images behind it. It will
 * automatically draw the supplied images for when it is raised, pressed or
 * highlighted. The images can be null and will not be drawn on those occasions.
 *
 * The paintRaised, paintPressed and paintHighlighted will be called
 * automatically by this buttons paint method. They can be overridden to add
 * custom drawing routines for those states.
 * 
 * @author Joseph Lenton
 */
public class ButtonImageActor<G extends GraphicsGL> extends ButtonActor<G>
{
    private GLTexture raisedImage;
    private GLTexture pressedImage;
    private GLTexture highlightedImage;

    /**
     * Trivial constructor where this has no images.
     */
    public ButtonImageActor()
    {
        this(null, null, null);
    }

    public ButtonImageActor(boolean useEventHandler)
    {
        this( useEventHandler, null, null, null);
    }

    /**
     * A button that will use the given images for when it is raised, pressed or
     * highlighted. If one of the given images is null then nothing will be
     * drawn for that state.
     * @param raisedImage
     * @param pressedImage
     * @param highlightedImage
     */
    public ButtonImageActor(GLTexture raisedImage, GLTexture pressedImage, GLTexture highlightedImage)
    {
        super();

        this.raisedImage = raisedImage;
        this.pressedImage = pressedImage;
        this.highlightedImage = highlightedImage;
    }
    
    public ButtonImageActor(boolean useEventHandler, GLTexture raisedImage, GLTexture pressedImage, GLTexture highlightedImage)
    {
        super( useEventHandler );

        this.raisedImage = raisedImage;
        this.pressedImage = pressedImage;
        this.highlightedImage = highlightedImage;
    }
    
    /**
     * @return 1 if this has no image, otherwise the current drawing width for the currently used image.
     */
    @Override
    public int getWidth()
    {
        final GLTexture currentImage = getCurrentImage();
        if (currentImage != null) {
            return currentImage.getWidth();
        } else {
            return 1;
        }
    }

    /**
     * @return The drawing height for the currently drawn image, or 1 if there is no image.
     */
    @Override
    public int getHeight()
    {
        final GLTexture currentImage = getCurrentImage();
        if (currentImage != null) {
            return currentImage.getHeight();
        } else {
            return 1;
        }
    }

    /**
     * @return The current image this button is using for drawing.
     */
    public GLTexture getCurrentImage()
    {
        if (isPressed()) {
            return getPressedImage();
        } else if (isHighlighted()) {
            return getHighlightedImage();
        } else {
            return getRaisedImage();
        }
    }

    /**
     * @return The highlight image, or null if there is no highlight image.
     */
    public GLTexture getHighlightedImage()
    {
        return highlightedImage;
    }

    /**
     * @return The image to draw when this button is pressed, or null for no image.
     */
    public GLTexture getPressedImage()
    {
        return pressedImage;
    }

    /**
     * @return The image to draw when this is raised, or null if there is no image.
     */
    public GLTexture getRaisedImage()
    {
        return raisedImage;
    }

    public void setHighlightedImage(GLTexture highlightedImage)
    {
        this.highlightedImage = highlightedImage;
    }

    public void setPressedImage(GLTexture pressedImage)
    {
        this.pressedImage = pressedImage;
    }

    public void setRaisedImage(GLTexture raisedImage)
    {
        this.raisedImage = raisedImage;
    }

    /**
     * If there is a raised image, it will drawn at this buttons location.
     * @param g The GraphicsGL to draw too.
     */
    @Override
    protected void paintRaised(G g)
    {
        paintImage(g, getRaisedImage());
    }

    /**
     * If there is a pressed image, it will drawn at this buttons location.
     * @param g The GraphicsGL to draw too.
     */
    @Override
    protected void paintPressed(G g)
    {
        paintImage(g, getPressedImage());
    }

    /**
     * If there is a highlight image, it will drawn at this buttons location.
     * @param g The GraphicsGL to draw too.
     */
    @Override
    protected void paintHighlighted(G g)
    {
        paintImage(g, getHighlightedImage());
    }

    /**
     * A helper method that draws the given image to the given graphics object.
     * It is drawn centered at the buttons current location, but only if it is
     * not null. If the image is null then nothing will occur.
     * @param g The GraphicsGL object the image will be drawn too, cannot be null.
     * @param image The image to draw centered to the graphics object, or null for no draw.
     */
    private void paintImage(G g, GLTexture image)
    {
        if (image != null) {
            g.drawTexture(image, getX(), getY(), true);
        }
    }
}
